#include "StdAfx.h"
#include "buddy_mgr_impl.h"
#include "global_data.h"
#include <interface/data_op/table_wrapper.h>
#include <interface/interface.h>
#include <utility/api_tools.h>
#include <interface/data_op/userinfo.h>
#include "res_str_mgr.h"

namespace aot{ namespace glb{

buddy_mgr_impl::buddy_mgr_impl(void)
{
    this->lock_.open();
}

buddy_mgr_impl::~buddy_mgr_impl()
{
    this->clear_buddy_list();
    this->lock_.close();
}

void 
buddy_mgr_impl::init_buddy_list()
{
	this->clear_buddy_list();
    aot::tt::iobj_auto_ptr<aot::data::iuser_op> user_op;

	if( !g_dll_obj->query_interface_from_data_op_dll(user_op, "iuser_op") )
		return;

	auto_lock_type __tmp_lock(this->lock_);
    aot::data::table_helper _table;

    user_op->buddy_get_all(_table);

	std::string user_id, user_name, group_name;
	aot::data::iuser_info* user_info = NULL;
	g_dll_obj->user_lst_mgr_->lock();
    for ( int i = 0; i < _table.get_rows_count(); i++ )
    {
        _table.set_row_index(i);

        user_id = _table.get_field_value_str("user_id");
		user_name = _table.get_field_value_str("nickname");
		group_name = _table.get_field_value_str("group_name");
		this->buddy_list_.cont_[user_id] = user_name;
		user_info = g_dll_obj->user_lst_mgr_->get_user_info(user_id.c_str());
		if ( user_info )
		{
			user_info->buddy_group_name->assign(group_name.c_str());
		}
    }
	g_dll_obj->user_lst_mgr_->unlock();
}

void 
buddy_mgr_impl::save_buddy_list()
{
	auto_lock_type __tmp_lock(this->lock_);
	g_dll_obj->user_lst_mgr_->save_to_db();
}

void 
buddy_mgr_impl::clear_buddy_list()
{
    auto_lock_type __tmp_lock(this->lock_);
    buddy_map_impl::map_type::iterator it;
    buddy_list_.cont_.clear();
}

bool 
buddy_mgr_impl::add_buddy(const char* user_id, aot::data::iuser_info* buddy_info)
{
    auto_lock_type __tmp_lock(this->lock_);

    buddy_map_impl::map_type::iterator it = buddy_list_.cont_.find(user_id);
	if ( it != buddy_list_.cont_.end())
	{
		it->second = buddy_info->nickname->c_str();
	}
	else
	{
		this->buddy_list_.cont_[user_id] = buddy_info->nickname->c_str();
	}

	g_dll_obj->user_lst_mgr_->set_user_info(user_id, buddy_info);
    return true;
}

void 
buddy_mgr_impl::remove_buddy(const char* user_id)
{
    auto_lock_type __tmp_lock(this->lock_);
    buddy_map_impl::map_type::iterator it;
    it = buddy_list_.cont_.find(user_id);
    if ( it != buddy_list_.cont_.end())
    {
        buddy_list_.cont_.erase(it);
    }
}

void 
buddy_mgr_impl::update_buddy(const char* user_id, aot::data::iuser_info* buddy_info)
{
    auto_lock_type __tmp_lock(this->lock_);
    buddy_map_impl::map_type::iterator it;
    it = buddy_list_.cont_.find(user_id);

	if ( it == buddy_list_.cont_.end())
	{
		it->second = buddy_info->nickname->c_str();
	}
	else
	{
		this->buddy_list_.cont_[user_id] = buddy_info->nickname->c_str();
	}

	g_dll_obj->user_lst_mgr_->set_user_info(user_id, buddy_info);

}

void 
buddy_mgr_impl::modify_group_name(const char* old_group_name, const char* new_group_name)
{
	auto_lock_type __tmp_lock(this->lock_);
	g_dll_obj->user_lst_mgr_->modify_group_name(old_group_name, new_group_name);
}


ibuddy_map* 
buddy_mgr_impl::get_buddy_list()
{
    auto_lock_type __tmp_lock(this->lock_);
    return (ibuddy_map*)&this->buddy_list_;
}

bool 
buddy_mgr_impl::find(const char* szBuddy_num)
{
    auto_lock_type __tmp_lock(this->lock_);
    buddy_map_impl::map_type::iterator it;
    it = buddy_list_.cont_.find(szBuddy_num);
    return ( it != buddy_list_.cont_.end());
}

bool 
buddy_mgr_impl::find(const char* szBuddy_num, aot::data::iuser_info** buddy_info)
{
    auto_lock_type __tmp_lock(this->lock_);

    buddy_map_impl::map_type::iterator it = buddy_list_.cont_.find(szBuddy_num);
    if ( it != buddy_list_.cont_.end())
    {
		return g_dll_obj->user_lst_mgr_->get_user_info(szBuddy_num, buddy_info);
    }

    return false;
}

bool 
buddy_mgr_impl::get_nickname(const char* user_id, aot::tt::istr** out)
{
    auto_lock_type __tmp_lock(this->lock_);
   
    buddy_map_impl::map_type::iterator it = buddy_list_.cont_.find(user_id);
    if ( it != buddy_list_.cont_.end() )
    {
		aot::tt::str_impl* impl = new aot::tt::str_impl();
		*out = impl;
        impl->str_ = it->second;
        return true;
    }
    return false;
}

bool 
buddy_mgr_impl::update_byname( const char* user_id, const char* name )
{
	auto_lock_type __tmp_lock(this->lock_);
	buddy_map_impl::map_type::iterator it;
	it = buddy_list_.cont_.find(user_id);
	if ( it != buddy_list_.cont_.end())
	{
		g_dll_obj->user_lst_mgr_->change_byname(user_id, name);
		return true;
	}
	return false;
	
}

bool 
buddy_mgr_impl::get_inner_id_list(aot::tt::ivec_ulong** out)
{
	aot::tt::ivec_ulong* v = new aot::tt::vec_ulong_impl;
	*out = v;

	auto_lock_type __tmp_lock(this->lock_);
	buddy_map_impl::map_type::iterator it = this->buddy_list_.cont_.begin();
	for( ; it != this->buddy_list_.cont_.end(); ++it )
	{
		unsigned long id = g_dll_obj->user_lst_mgr_->get_user_inner_id(it->first.c_str());
		if( id != 0 )
		{
			v->push_back(id);
		}
	}
	return true;
}

 }} /* end namespace aot/glb*/
